package com.luoguijia.disk.login.control;

import com.luoguijia.disk.login.service.PasswordResetService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

@Controller
@RequestMapping("/passwordReset")
public class PasswordResetController {

    @Resource
    private PasswordResetService passwordResetService;

    /**
     * 跳转到密码重置页面
     */
    @RequestMapping("/page")
    public ModelAndView passwordResetPage() {
        return new ModelAndView("login/passwordResetPage");
    }

    /**
     * 发送验证码邮件
     */
    @RequestMapping(value = "/sendCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> sendCode(String email) {
        return passwordResetService.sendResetPasswordCode(email);
    }

    /**
     * 验证验证码并重置密码
     */
    @RequestMapping(value = "/confirm", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> confirm(String email, String code, String newPassword) {
        return passwordResetService.resetPassword(email, code, newPassword);
    }
}